//
//  LiveHandle.m
//  BasketballClub
//
//  Created by lanou3g on 16/1/6.
//  Copyright © 2016年 孙鸿吉. All rights reserved.
//

#import "LiveHandle.h"
#import <AFNetworking.h>
#import "LiveModel.h"
#import "CoreDataManager.h"
static LiveHandle *handle = nil;
@implementation LiveHandle
//单例的初始化
+(instancetype)sharedLiveHandle
{
    return [[self alloc]init];
}
+(instancetype)allocWithZone:(struct _NSZone *)zone
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (handle == nil) {
            handle = [super allocWithZone:zone];
        }
        
    });
    return handle;
   
}


#pragma mark - 请求数据的方法
///昨日
-(void)GetYesterdayDataFromNetWorkWithBlock:(LIVEBLOCK)finishBlock
{
     //创建管理类
    AFHTTPSessionManager *sessionManager = [AFHTTPSessionManager manager];
    //设置返回方式
    sessionManager.responseSerializer = [AFJSONResponseSerializer serializer];
    
    [sessionManager GET:@"http://op.juhe.cn/onebox/basketball/nba?key=476cbbdbfb38f73accf694e4abb6b6b9" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (responseObject != nil) {
            //转化数据格式
            NSDictionary *dict = (NSDictionary*)responseObject;
            //提取数据字典中的关键字段
            NSDictionary *resultDic = dict[@"result"];
            //提取数据中的关键字段
            
            NSArray *array = resultDic[@"list"];
            //提取数组中的第1个元素
            NSDictionary *yesterdayDict = array[0];
            
            if (self.liveYesterdayDataArray == nil) {
                self.liveYesterdayDataArray = [NSMutableArray array];
            }
            
            NSArray *tempArr = yesterdayDict[@"tr"];
            //遍历数组
            for (NSDictionary *dic in tempArr) {
                //KVC赋值
                LiveModel *model = [[LiveModel alloc]init];
                
                [model setValuesForKeysWithDictionary:dic];
                //把数据储存到数组
                [self.liveYesterdayDataArray addObject:model];
                if ([[CoreDataManager shareCoreDateManager] selectAllYesterdayModel].count > self.liveYesterdayDataArray.count) {
                    [[CoreDataManager shareCoreDateManager] deleteYesterdayAllData];
                }else{
                    [[CoreDataManager shareCoreDateManager] insertYesterdayDataToSql:model];
                }
                
                
            }
            
        }else{
            
        }

        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        self.liveYesterdayDataArray = [NSMutableArray arrayWithArray:[[CoreDataManager shareCoreDateManager] selectAllYesterdayModel]];
        
        

    }];
    
    
    
//    //创建字典
//    NSDictionary *dictionary = [NSDictionary dictionaryWithObject:@"476cbbdbfb38f73accf694e4abb6b6b9" forKey:@"key"];
//    //对数据进行POST请求
//        [sessionManager POST:@"http://op.juhe.cn/onebox/basketball/nba?" parameters:dictionary progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//            if (responseObject != nil) {
//                //转化数据格式
//                NSDictionary *dict = (NSDictionary*)responseObject;
//                //提取数据字典中的关键字段
//                NSDictionary *resultDic = dict[@"result"];
//                //提取数据中的关键字段
//                
//                NSArray *array = resultDic[@"list"];
//                //提取数组中的第1个元素
//                NSDictionary *yesterdayDict = array[0];
//                
//                if (self.liveYesterdayDataArray == nil) {
//                    self.liveYesterdayDataArray = [NSMutableArray array];
//                }
//                
//                NSArray *tempArr = yesterdayDict[@"tr"];
//                //遍历数组
//                for (NSDictionary *dic in tempArr) {
//                    //KVC赋值
//                    LiveModel *model = [[LiveModel alloc]init];
//                    
//                    [model setValuesForKeysWithDictionary:dic];
//                    //把数据储存到数组
//                    [self.liveYesterdayDataArray addObject:model];
//                    if ([[CoreDataManager shareCoreDateManager] selectAllYesterdayModel].count > self.liveYesterdayDataArray.count) {
//                        [[CoreDataManager shareCoreDateManager] deleteYesterdayAllData];
//                    }else{
//                        [[CoreDataManager shareCoreDateManager] insertYesterdayDataToSql:model];
//                    }
//                    
//                    
//                }
//
//            }else{
//                
//            }
//                     
//    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//        self.liveYesterdayDataArray = [NSMutableArray arrayWithArray:[[CoreDataManager shareCoreDateManager] selectAllYesterdayModel]];
//        
//        
//    }];
    //回到主线程
    dispatch_async(dispatch_get_main_queue(), ^{
        
        if (self.liveYesterdayDataArray.count> 0) {
            
            finishBlock(YES);
            
        }else{
            
            finishBlock(NO);
        }
        
    });
    
}
#pragma mark - 请求数据的方法
///今日
-(void)GetTodayDataFromNetWorkWithBlock:(LIVEBLOCK)finishBlock
{
    //创建管理类
    AFHTTPSessionManager *sessionManager = [AFHTTPSessionManager manager];
    //设置数据返回格式
    sessionManager.responseSerializer = [AFJSONResponseSerializer serializer];
    //创建字典
    [sessionManager GET:@"http://op.juhe.cn/onebox/basketball/nba?key=476cbbdbfb38f73accf694e4abb6b6b9" parameters:nil progress:nil
                success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                    if (responseObject != nil) {
                        //转化数据格式
                        NSDictionary *dict = (NSDictionary*)responseObject;
                        //提取数据字典中的关键字段
                        NSDictionary *resultDic = dict[@"result"];
                        //提取数据中的关键字段
                        NSArray *array = resultDic[@"list"];
                        //提取数组中的第二个元素
                        NSDictionary *yesterdayDict = array[1];
                        
                        //遍历数组
                        if (self.liveTodayDataArray ==nil) {
                            self.liveTodayDataArray = [NSMutableArray array];
                        }
                        NSArray *tempArr = yesterdayDict[@"tr"];
                        
                        for (NSDictionary *dic in tempArr) {
                            //KVC赋值
                            LiveModel *model = [[LiveModel alloc]init];
                            
                            [model setValuesForKeysWithDictionary:dic];
                            //把数据储存到数组
                            [self.liveTodayDataArray addObject:model];
                            
                            
                            if ([[CoreDataManager shareCoreDateManager] selectAllTodayModel].count > self.liveTodayDataArray.count) {
                                [[CoreDataManager shareCoreDateManager] deleteTodayAllData];
                            }else{
                                [[CoreDataManager shareCoreDateManager] insertTodayDataToSql:model];
                            }
                            
                        }
                    }else{
                        
                    }
                    
                    

                } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                     self.liveTodayDataArray = [NSMutableArray arrayWithArray:[[CoreDataManager shareCoreDateManager] selectAllTodayModel]];
                }];
    
    
    
//    NSDictionary *dictionary = [NSDictionary dictionaryWithObject:@"476cbbdbfb38f73accf694e4abb6b6b9" forKey:@"key"];
//    //对数据进行POST请求
//    [sessionManager POST:@"http://op.juhe.cn/onebox/basketball/nba?" parameters:dictionary progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        if (responseObject != nil) {
//            //转化数据格式
//            NSDictionary *dict = (NSDictionary*)responseObject;
//            //提取数据字典中的关键字段
//            NSDictionary *resultDic = dict[@"result"];
//            //提取数据中的关键字段
//            NSArray *array = resultDic[@"list"];
//            //提取数组中的第二个元素
//            NSDictionary *yesterdayDict = array[1];
//            
//            //遍历数组
//            if (self.liveTodayDataArray ==nil) {
//                self.liveTodayDataArray = [NSMutableArray array];
//            }
//            NSArray *tempArr = yesterdayDict[@"tr"];
//            
//            for (NSDictionary *dic in tempArr) {
//                //KVC赋值
//                LiveModel *model = [[LiveModel alloc]init];
//                
//                [model setValuesForKeysWithDictionary:dic];
//                //把数据储存到数组
//                [self.liveTodayDataArray addObject:model];
//                
//                
//                if ([[CoreDataManager shareCoreDateManager] selectAllTodayModel].count > self.liveTodayDataArray.count) {
//                    [[CoreDataManager shareCoreDateManager] deleteTodayAllData];
//                }else{
//                    [[CoreDataManager shareCoreDateManager] insertTodayDataToSql:model];
//                }
//                
//            }
//        }else{
//            
//        }
//        
//        
//    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//        self.liveTodayDataArray = [NSMutableArray arrayWithArray:[[CoreDataManager shareCoreDateManager] selectAllTodayModel]];
//        
//        
//    }];
    //回到主线程
    dispatch_async(dispatch_get_main_queue(), ^{
        
        if (self.liveTodayDataArray.count> 0) {
            
            finishBlock(YES);
            
        }else{
            
            finishBlock(NO);
        }
        
    });
    
}


#pragma mark - 请求数据的方法
///明日
-(void)GetTomorrowDataFromNetWorkWithBlock:(LIVEBLOCK)finishBlock
{
    //创建管理类
    AFHTTPSessionManager *sessionManager = [AFHTTPSessionManager manager];
    //设置返回方式
    sessionManager.responseSerializer = [AFJSONResponseSerializer serializer];
    
    [sessionManager GET:@"http://op.juhe.cn/onebox/basketball/nba?key=476cbbdbfb38f73accf694e4abb6b6b9" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (responseObject != nil) {
            //转化数据格式
            NSDictionary *dict = (NSDictionary*)responseObject;
            //提取数据字典中的关键字段
            NSDictionary *resultDic = dict[@"result"];
            //提取数据中的关键字段
            NSArray *array = resultDic[@"list"];
            //提取数组中的第3个元素
            NSDictionary *yesterdayDict = array[2];
            
            //创建临时可变数组
            if (self.liveTomorrowDataArray == nil) {
                self.liveTomorrowDataArray = [NSMutableArray array];
            }
            NSArray *tempArr = yesterdayDict[@"tr"];
            //遍历数组
            for (NSDictionary *dic in tempArr) {
                
                LiveModel *model = [[LiveModel alloc]init];
                
                [model setValuesForKeysWithDictionary:dic];
                //把数据储存到数组
                [self.liveTomorrowDataArray addObject:model];
                if ([[CoreDataManager shareCoreDateManager] selectAllTomorrowModel].count > self.liveTomorrowDataArray.count) {
                    [[CoreDataManager shareCoreDateManager] deleteTomorrowAllData];
                }else{
                    [[CoreDataManager shareCoreDateManager] insertTomorrowDataToSql:model];
                }
            }
            
        }else{
            
        }
        
        
        

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
         self.liveTomorrowDataArray = [NSMutableArray arrayWithArray:[[CoreDataManager shareCoreDateManager] selectAllTomorrowModel]];
    }];
    
    
//    //创建字典
//    NSDictionary *dictionary = [NSDictionary dictionaryWithObject:@"476cbbdbfb38f73accf694e4abb6b6b9" forKey:@"key"];
//    //对数据进行POST请求
//    [sessionManager POST:@"http://op.juhe.cn/onebox/basketball/nba?" parameters:dictionary progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        if (responseObject != nil) {
//            //转化数据格式
//            NSDictionary *dict = (NSDictionary*)responseObject;
//            //提取数据字典中的关键字段
//            NSDictionary *resultDic = dict[@"result"];
//            //提取数据中的关键字段
//            NSArray *array = resultDic[@"list"];
//            //提取数组中的第3个元素
//            NSDictionary *yesterdayDict = array[2];
//            
//            //创建临时可变数组
//            if (self.liveTomorrowDataArray == nil) {
//                self.liveTomorrowDataArray = [NSMutableArray array];
//            }
//            NSArray *tempArr = yesterdayDict[@"tr"];
//            //遍历数组
//            for (NSDictionary *dic in tempArr) {
//                
//                LiveModel *model = [[LiveModel alloc]init];
//                
//                [model setValuesForKeysWithDictionary:dic];
//                //把数据储存到数组
//                [self.liveTomorrowDataArray addObject:model];
//                if ([[CoreDataManager shareCoreDateManager] selectAllTomorrowModel].count > self.liveTomorrowDataArray.count) {
//                    [[CoreDataManager shareCoreDateManager] deleteTomorrowAllData];
//                }else{
//                    [[CoreDataManager shareCoreDateManager] insertTomorrowDataToSql:model];
//                }
//            }
//
//        }else{
//            
//        }
//        
//                 
//        
//    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//        
//        self.liveTomorrowDataArray = [NSMutableArray arrayWithArray:[[CoreDataManager shareCoreDateManager] selectAllTomorrowModel]];
//        
//    }];
    //回到主线程
    dispatch_async(dispatch_get_main_queue(), ^{
        
        if (self.liveTomorrowDataArray.count> 0) {
            
            finishBlock(YES);
            
        }else{
            
            finishBlock(NO);
        }
        
    });
    
}


///请求数据
-(void)getDataFromNetWorkWithBlock:(LIVEBLOCK)finishBlock
{
    //创建管理类
    AFHTTPSessionManager *sessionManager = [AFHTTPSessionManager manager];
    //设置返回方式
    sessionManager.responseSerializer = [AFJSONResponseSerializer serializer];
    
    [sessionManager GET:self.urlStr parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        //转化数据格式
        NSDictionary *dict = (NSDictionary*)responseObject;
        //提取数据字典中的关键字段
        NSDictionary *resultDic = dict[@"result"];
        //提取数据中的关键字段
        NSArray *array = resultDic[@"list"];
       
        //创建临时可变数组
        NSMutableArray *tempArray  = [NSMutableArray array];
//
        //遍历数组
        for (NSDictionary *dic in array) {
            
            LiveModel *model = [[LiveModel alloc]init];
            //KVC赋值
            [model setValuesForKeysWithDictionary:dic];
            //把 model添加到可变数组
            [tempArray addObject:model];
           
        }
       // 把数据储存到数组
        self.battleDetailsArray = [NSArray arrayWithArray:tempArray];
       
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
    
    //回到主线程
    dispatch_async(dispatch_get_main_queue(), ^{
        
        if (self.battleDetailsArray.count> 0) {
            
            finishBlock(YES);
            
        }else{
            
            finishBlock(NO);
        }
        
    });


}


-(void)getDataFromNetWorkWithUrlStr:(NSString*)urlString withBlock:(LIVEBLOCK)finishBlock
{
    //创建管理类
    AFHTTPSessionManager *sessionManager = [AFHTTPSessionManager manager];
    //设置返回方式
    sessionManager.responseSerializer = [AFJSONResponseSerializer serializer];
    
    [sessionManager GET:urlString parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        //转化数据格式
        NSDictionary *dict = (NSDictionary*)responseObject;
        //提取数据字典中的关键字段
        NSDictionary *resultDic = dict[@"result"];
        //提取数据中的关键字段
        NSArray *array = resultDic[@"list"];
        
        //创建临时可变数组
        NSMutableArray *tempArray  = [NSMutableArray array];
        //
        //遍历数组
        for (NSDictionary *dic in array) {
            
            LiveModel *model = [[LiveModel alloc]init];
            
            [model setValuesForKeysWithDictionary:dic];
        
            [tempArray addObject:model];
            
        }
        // 把数据储存到数组
        self.battleDetailsArray = [NSArray arrayWithArray:tempArray];
               
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
    //回到主线程
    dispatch_async(dispatch_get_main_queue(), ^{
        
        if (self.battleDetailsArray.count> 0) {
            
            finishBlock(YES);
            
        }else{
            
            finishBlock(NO);
        }
        
    });
}








@end
